import {defineStore} from "pinia";
import {ref} from 'vue'
import {useUserStoreStore} from "@/stores/userStoreStore";


export const useShopListStore=defineStore('shopList',()=>{

    let shopList=ref([])
    const userStoreStore=useUserStoreStore();

    const getShopList=()=>{
        return shopList.value
    }

    const clearShopListStore=()=>{
        shopList.value=[]
    }

    const getFirstShopList=()=>{

        if(!shopList.value.length)
        {
            shopList.value.push({store:null,list:[]})
        }
        return shopList.value[0]
    }

    const clearFirstShopList=()=>{
        if(shopList.value.length)
        {
            shopList.value[0]={}
        }
    }

    const initFirstShopList=(store)=>{
        //初始化
        if(shopList.value.length)
        {
            shopList.value[0].store=store
            shopList.value[0].list=[]
        }
        else
        {
            shopList.value.push({list:[],store:store})
        }
    }

    const changeFirstGoods=(index,data)=>{
        shopList.value[0].list[index]=JSON.parse(JSON.stringify(data))

        if(shopList.value[0].list[index].count===0)
        {
            //移出去
            shopList.value[0].list=shopList.value[0].list.filter((item,i)=>{
                if(i!==index) return item
            })
        }
    }

    const setFirstStore=(data)=>{
        if(shopList.value.length)
        {
            shopList.value[0].store=data
        }
        else
        {
            shopList.value.push({store:data,list:[]})
        }
    }

    const addToFirstShopList=(newGoods)=>{
        console.log(newGoods)
        console.log(shopList.value[0])
        for (let i = 0; i < newGoods.flavors.length; i++) {
            newGoods.goods.price+=newGoods.flavors[i].price
        }

        if(shopList.value.length)
        {
            if(!shopList.value[0].list)
            {
                shopList.value[0]={store:null,list:[]}
                shopList.value[0].list.push(newGoods)
            }
            else
            {
                for (let i = 0; i < shopList.value[0].list.length; i++) {
                    if(JSON.stringify({flavors:shopList.value[0].list[i].flavors,goods:shopList.value[0].list[i].goods})
                        ===JSON.stringify({flavors:newGoods.flavors,goods:newGoods.goods}))
                    {
                        shopList.value[0].list[i].count++
                        return
                    }
                }
                shopList.value[0].list.push(newGoods)
            }
        }
        else
        {
            shopList.value.push({store:null,list:[]})
        }
    }

    const shopCartToPay=(data)=>{
        //购物车的内容添加到这里
        shopList.value=[]
        shopList.value.push(...data)
    }

    const getTotalSum=()=>{
        let sum=0;
        for (let i = 0; i < shopList.value.length; i++) {
            if(shopList.value[i].list)
            {
                for (let j = 0; j < shopList.value[i].list.length; j++) {
                    sum+=shopList.value[i].list[j].count;
                }
            }
        }
        return sum
    }

    const getOneTotalCount=(index)=>{
        let sum=0;
        for (let i = 0; i < shopList.value[index].list.length; i++) {
            sum+=shopList.value[index].list[i].count
        }
        return sum
    }

    const getOneStorePrice=(index)=>{
        let sum=0;
        for (let i = 0; i < shopList.value[index].list.length; i++) {
            sum+=shopList.value[index].list[i].goods.price*shopList.value[index].list[i].count
        }
        sum+=getOneTotalCount(index)*shopList.value[index].store.packageFee
        return sum
    }
    
    const getAllPackageFee=()=>{
        let sum=0
        for (let i = 0; i < shopList.value.length; i++) {
            let count=0
            if(shopList.value[i].list)
            {
                for (let j = 0; j < shopList.value[i].list.length; j++) {
                    count+=shopList.value[i].list[j].count;
                }
            }
            if(shopList.value[i].store)  sum+=count*shopList.value[i].store.packageFee
        }

        return sum
    }

    const getOneAllPackage=(index)=>{
        let sum=0;
        for (let i = 0; i < shopList.value[index].list.length; i++) {
            sum+=shopList.value[index].list.count
        }
        return sum
    }

    const getTotalPrice=()=>{
        let sum=0
        for (let i = 0; i < shopList.value.length; i++) {
            if(shopList.value[i].list.length)
            {
                sum+=getOneStorePrice(i);
            }
        }

        return sum
    }


    return {
        shopList,
        getShopList,
        getFirstShopList,
        getAllPackageFee,
        getTotalSum,
        getTotalPrice,
        initFirstShopList,
        addToFirstShopList,
        shopCartToPay,
        clearFirstShopList,
        changeFirstGoods,
        setFirstStore,
        getOneStorePrice,
        getOneAllPackage,
        getOneTotalCount,
        clearShopListStore
    }

},{
    persist:true
})

// export const useShopListStore=defineStore('shopList',()=>{
//     //这里是存储 当前店铺的 一些数据
//     let shopList=ref([])
//     const userStoreStore=useUserStoreStore();
//
//     const getShopList=()=>{
//         return shopList.value
//     }
//
//     const clearSelected=()=>{
//         shopList.value=shopList.value.filter((item)=>{
//             if(!item.isSelected) return item
//         })
//     }
//
//     const clearShopList=()=>{
//         shopList.value=[]
//     }
//
//     //添加到第一个
//     const addToListFirst=(newGoods)=>{
//         //设置价格
//         for (let i = 0; i < newGoods.flavors.length; i++) {
//             newGoods.goods.price+=newGoods.flavors[i].price
//         }
//
//         for (let i = 0; i < shopList.value[0].list.length; i++) {
//             if(JSON.stringify(shopList.value[0][i])===JSON.stringify(newGoods))
//             {
//                 shopList.value[0][i].count++
//                 return
//             }
//         }
//
//         shopList.value[0][i].push(newGoods)
//     }
//
//     const addToList=(newGoods)=>{
//
//         //设置价格
//         for (let i = 0; i < newGoods.flavors.length; i++) {
//             newGoods.goods.price+=newGoods.flavors[i].price
//         }
//
//         for (let i = 0; i < shopList.value.length; i++) {
//             if(JSON.stringify(shopList.value[i])===JSON.stringify(newGoods))
//             {
//                 shopList.value[i].count++
//                 return
//             }
//         }
//
//         shopList.value.push(newGoods)
//     }
//
//     const getSum=()=>{
//         return shopList.value.reduce((sum,item)=>sum+item.count,0);
//     }
//
//     const getAllPackageFee=()=>{
//         return getSum()*userStoreStore.storePackageFee
//     }
//
//     const deleteFromList = (id) => {
//         shopList.value=shopList.value.filter((item)=>{
//             if(item.goods.id!==id) return item
//             else {
//                 console.log("找到了", item.goods.id,id)
//             }
//         })
//         console.log(shopList.value)
//     };
//
//     const changeGoods = (id, good) => {
//         console.log(id);
//         const updatedList = shopList.value.map((item) => {
//             if (item.goods.id === id) {
//                 // 如果找到匹配的商品，返回新的商品对象
//                 return good;
//             }
//             return item;
//         });
//
//         shopList.value = updatedList;
//
//         console.log(shopList.value);
//
//         // 在修改商品后检查是否需要删除
//         if (good.count === 0) {
//             console.log("当前数量为0");
//             deleteFromList(good.goods.id);
//         }
//     };
//
//     // const watchData(good)
//
//
//     const getTotalPrice=()=>{
//         return shopList.value.reduce((acc, item) => acc + item.goods.price * item.count, 0)+getAllPackageFee()
//     }
//
//     return {
//         shopList,
//         getShopList,
//         clearShopList,
//         clearSelected,
//         getSum,
//         addToList,
//         deleteFromList,
//         changeGoods,
//         getTotalPrice,
//         getAllPackageFee
//     }
// },{
//     persist:true
// })